﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using DocumentFormat.OpenXml;

namespace ExtremeML.Core
{
	/// <summary>
	/// Serves as the generic base class for classes that wrap package parts or elements.
	/// </summary>
	/// <typeparam name="TWrapped">A type that derives from <see cref="OpenXmlElement"/>.</typeparam>
	public abstract class ItemWrapper<TWrapped> : WrapperBase<TWrapped>, IItemWrapper
	{
		#region Housekeeping

		/// <summary>
		/// Initializes a new instance of the <see cref="ItemWrapper{TWrapped}"/> class.
		/// </summary>
		/// <param name="wrapped">A document content object from which to populate this instance.</param>
		/// <param name="parent">An <see cref="IContentWrapper"/> that represents the parent wrapper instance.</param>
		/// <param name="package">An <see cref="IContentWrapper"/> that represents the package wrapper instance.</param>
		protected ItemWrapper(
			TWrapped wrapped,
			IContentWrapper parent,
			IContentWrapper package)
			: base(wrapped, parent, package)
		{
		}

		#endregion
		#region Properties


		#endregion
		#region Overrides


		#endregion
		#region Life-Cycle Methods

		/// <summary>
		/// When overridden in a derived custom partial class, declares custom wrapper
		/// persistence code that will execute before generated persistence code.
		/// </summary>
		protected virtual void OnCustomRecomposing(TWrapped wrapped)
		{
		}

		/// <summary>
		/// When overridden in a derived custom partial class, declares custom wrapper
		/// persistence code that will execute after generated persistence code.
		/// </summary>
		protected virtual void OnCustomRecomposed(TWrapped wrapped)
		{
		}

		/// <summary>
		/// When overridden in a derived custom partial class, performs
		/// housekeeping and validation of a requested deletion.
		/// </summary>
		protected virtual bool OnCustomRemoving()
		{
			return true;
		}

		/// <summary>
		/// When overridden in a derived custom partial class, performs
		/// housekeeping after a deletion.
		/// </summary>
		protected virtual void OnCustomRemoved()
		{
		}

		#endregion
		#region Content Management Methods

		/// <summary>
		/// When overridden in a derived class, removes the instance,
		/// subject to its context within the wrapper hierarchy.
		/// </summary>
		internal abstract void Remove();

		#endregion
		#region IItemWrapper Implementation

		void IItemWrapper.Remove()
		{
			Remove();
		}

		#endregion
	}
}
